package com.wuliu.dao.impl;


import com.wuliu.model.NewsClass;
import com.wuliu.util.Jdbc;
import com.wuliu.dao.NewsClassDAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


public class NewsClassDAOImpl implements NewsClassDAO {
    @Override
    public void add(NewsClass newsClass) {
        try {
            Jdbc jdbc=new Jdbc();
            Connection connection=jdbc.connection();
            String sql="insert into news_class values(null,?,?)";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setString(1,newsClass.getName());
            statement.setString(2,newsClass.getImg());
            statement.execute();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteById(int id) {
        try {
            Jdbc jdbc=new Jdbc();
            Connection connection=jdbc.connection();
            String sql="delete from news_class where id=?";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setInt(1,id);
            statement.execute();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void update(NewsClass newsClass) {
        try {
            Jdbc jdbc=new Jdbc();
            Connection connection=jdbc.connection();
            String sql="update news_class set name=? ,img=?where id=?";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setString(1,newsClass.getName());
            statement.setString(2,newsClass.getImg());

            statement.setInt(3,newsClass.getId());


            statement.execute();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public NewsClass findById(int id) {
        NewsClass newsClass=null;
        try {
            Jdbc jdbc=new Jdbc();
            Connection connection=jdbc.connection();
            String sql="select * from news_class where id=?";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setInt(1,id);
            ResultSet set=statement.executeQuery();
            while (set.next()){
                String name=set.getString("name");
                String img =set.getString("img");
                newsClass=new NewsClass(id,name,img);
            }
            set.close();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return newsClass;

    }

    @Override
    public List<NewsClass> findAll() {
        List<NewsClass> newsClasses=new ArrayList<>();
        try {
            Jdbc jdbc=new Jdbc();
            Connection connection=jdbc.connection();
            String sql="select * from news_class";
            PreparedStatement statement=connection.prepareStatement(sql);
            ResultSet set=statement.executeQuery();
            while (set.next()){
                int id=set.getInt("id");
                String name =set.getString("name");
                String img =set.getString("img");
                NewsClass newsClass=new NewsClass(id,name,img);
                newsClasses.add(newsClass);
            }
            set.close();
            statement.close();
            connection.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return newsClasses;
    }
}
